import request from '../../utils/request'

Page({
  data: {
    loading: true,
    id: null,
    sourceImageKey: '',
    targetImageKey: '',
    textContent: '',
    newText: ''
  },

  onLoad() {
    this.fetchRandomMaterial()
    // this.onTagTap1()
  },

  fetchRandomMaterial() {
    this.setData({ loading: true })
    request({
      url: 'material/random-empty2',
      method: 'GET'
    }).then(res => {
      const data = res.data
      this.setData({
        id: data.id,
        sourceImageKey: data.sourceImage.url,
        targetImageKey: data.targetImage.url,
        textContent: data.textContent || '',
        loading: false
      })
    })
    .catch(err => {
      this.setData({ loading: false })
      wx.hideLoading()
      setTimeout(() => {
        wx.showToast({
          title: err.message || '请求失败',
          icon: 'none',
          duration: 3000
        })
      }, 100)
    })
  },

  onInput(e) {
    this.setData({ newText: e.detail.value })
  },

  onSubmit() {
    const { id, newText } = this.data
    if (!id) {
      wx.showToast({
        title: '计划已终止！',
        icon: 'none'
      })
      return
    }

    if (!newText.trim()) {
      wx.showToast({ title: '请输入新文本', icon: 'none' })
      return
    }

    request({
      url: 'material/update-content2',
      method: 'PUT',
      data: {
        id,
        textContent: newText
      }
    }).then(() => {
      wx.showToast({ title: '提交成功', icon: 'success' })
      this.setData({ newText: '' })
      this.fetchRandomMaterial()
      // this.onTagTap1()
    }).catch(() => {
      wx.showToast({ title: '提交失败', icon: 'none' })
    })
  },

  // 花瓣
  onTagTap1() {
    this.setData({
      newText: 'Cut the edges of the four hole circular button along the wavy curve with the center symmetrical point as the reference, forming continuous petal shaped waves. Polish and remove burrs to restore luster, and obtain a lace button.'
    });
  },

  // 不规则
  onTagTap2() {
    this.setData({
      newText: 'Cut the edge of the four-hole circular button into an asymmetrical and irregular shape, removing one or more sectors or arcs. The central area with four holes remains unchanged.' // 示例文本，可替换为其他固定文案
    });
  },

  // 六角星
  onTagTap3() {
    this.setData({
      newText: 'Cut the edges of the four-hole circular button into a symmetrical hexagram shape using the center point as reference. Maintain the central four-hole pattern while forming six sharp star points through precision contour cutting. ' // 示例文本，可替换为其他固定文案
    });
  },

    // 四方形
    onTagTap4() {
      this.setData({
        newText: 'By cutting, the circular button is accurately transformed into a square contour, while retaining the four small holes arranged symmetrically in the center.' // 示例文本，可替换为其他固定文案
      });
    },
    // 心形
    onTagTap5() {
      this.setData({
        newText: 'Accurately cut the circular button into a full and rounded heart-shaped contour, while retaining the four small holes symmetrically arranged in the center.' // 示例文本，可替换为其他固定文案
      });
    },
    // 星形
    onTagTap6() {
      this.setData({
        newText: 'Precision radial cutting converts the circular button into an 8-point starburst pattern while maintaining the original 4-hole symmetrical configuration. Each faceted point extends at 45-degree intervals from the central axis.' // 示例文本，可替换为其他固定文案
      });
    },
    onTagTap7() {
      this.setData({
        newText: 'Precision cutting transforms the circular button into a five-pointed star contour with equal-length sides. Five 72°-angled facets are cut symmetrically from the center, while maintaining the original four-hole square pattern. Each star point features polished edges at 54° bevel for enhanced durability.' // 示例文本，可替换为其他固定文案
      });
    },

    // 齿轮
    //Precision cutting converts the smooth circular knob into a toothed gear profile. Twelve 30°-spaced teeth are milled equidistantly along the circumference while preserving the central cross-shaped engraving. Each tooth features 55° flank angles with polished root fillets for smooth meshing.

    // 雕刻



  
  onReject() {
    const { id } = this.data
    if (!id) {
      wx.showToast({
        title: '计划已终止！',
        icon: 'none'
      })
      return
    }

    request({
      url: 'material/update-content2',
      method: 'PUT',
      data: {
        id,
        textContent: '不合适'
      }
    }).then(() => {
      wx.showToast({ title: '提交成功', icon: 'success' })
      this.setData({ newText: '' })
      this.fetchRandomMaterial()
      // this.onTagTap1()

    }).catch(() => {
      wx.showToast({ title: '提交失败', icon: 'none' })
    })
  },

  onPreviewImage(e) {
    const current = e.currentTarget.dataset.src
    if (!current) {
      wx.showToast({ title: '无效图片', icon: 'none' })
      return
    }

    wx.previewImage({
      current,
      urls: [current]
    })
  }
})
